﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using Mortgage.DAL.Entity;

namespace Mortgage.Form
{
    public partial class ReportExpiredMortgage : System.Web.UI.Page
    {
        const string RDLC_PATH = "~/Rpt/rptMortByStatus.rdlc";
        const string RDLC_BLANK_PATH = "~/Rpt/rptBlank.rdlc";
        Mortgage.Srv.IReportSrv svcReport = Mortgage.Srv.SrvFactory.CreateReportSrv();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BlankReport();
                BindMortStatus();
            }
        }

        private void BlankReport()
        {
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "dsReport";
            rds.Value = new List<vwMortgageInfo>();


            rptViewer.LocalReport.ReportPath = Server.MapPath(RDLC_BLANK_PATH);
            rptViewer.LocalReport.DataSources.Clear();
            //rptViewer.LocalReport.DataSources.Add(rds);

            rptViewer.LocalReport.EnableExternalImages = true;


            rptViewer.LocalReport.Refresh();
        }

        protected void btnRunReport_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                rptViewer.Reset();
                rptViewer.LocalReport.Dispose();

                DateTime dteStar = new DateTime();
                DateTime dteEnd = new DateTime();
                Utility.ParseDateTime(fldStart.Text, out dteStar);
                Utility.ParseDateTime(fldEnd.Text, out dteEnd);
                int ageId = Utility.AgentId;

                string status = drpStatus.SelectedItem.Text;
                int? statusId = null;
                if (drpStatus.SelectedIndex > 0)
                {
                    statusId = Convert.ToInt32(drpStatus.SelectedValue);
                }

                List<vwMortgageInfo> dtSource = svcReport.GetMortgageByStatus(dteStar, dteEnd, ageId, statusId);

                ReportDataSource rds = new ReportDataSource();
                rds.Name = "dsReport";
                rds.Value = dtSource;

                rptViewer.LocalReport.Dispose();

                rptViewer.LocalReport.ReportPath = Server.MapPath(RDLC_PATH);
                rptViewer.LocalReport.DataSources.Clear();
                rptViewer.LocalReport.DataSources.Add(rds);

                rptViewer.LocalReport.EnableExternalImages = true;

                rptViewer.LocalReport.Refresh();

                Srv.IAgentSrv svcAgent = Srv.SrvFactory.CreateAgentSrv();
                tAgent agent = svcAgent.GetById(ageId);

                ReportParameter rptParAgeId = new ReportParameter("parAgent", agent.Name);
                ReportParameter rptParStart = new ReportParameter("parStart", dteStar.ToShortDateString());
                ReportParameter rptParEnd = new ReportParameter("parEnd", dteEnd.ToShortDateString());
                ReportParameter rptParStatus = new ReportParameter("parStatus", status);

                rptViewer.LocalReport.SetParameters(new ReportParameter[] { rptParAgeId, rptParStart, rptParEnd, rptParStatus });

                rptViewer.LocalReport.Refresh();


            }


        }

        protected void BindMortStatus()
        {
            drpStatus.DataSource = Utility.GetMortgageStatus();
            drpStatus.DataBind();
        }

    }
}